package com.penghaisoft.microwms.base.web.expose.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.penghaisoft.microwms.base.web.data.service.IMdModelService;
import com.penghaisoft.microwms.base.web.data.service.IMdProModelService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdModel;
import com.penghaisoft.microwms.common.data.model.MdModelMes;
import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;

/**
 * @Description 产品型号
 * @author luoteng
 * @date 2018年5月29日 下午2:33:39
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/expose/data/model")
@Slf4j
public class ExposeMdModelController extends BaseController {
	@Autowired
	private IMdModelService mdModelService;

	@Autowired
	private IMesInterfaceService mesInterfaceService;

	@Resource
	private MesProperties mesProperties;
	
	@Autowired
	private IMdProModelService mdProModelService;

	/**
	 * 新增记录
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(MdModel mdModel) {
//		自动转存大写
		mdModel.setModel(mdModel.getModel().toUpperCase());
		Date now = new Date();
		TransResult result = new TransResult();

		MdModel condition = new MdModel();
		condition.setModel(mdModel.getModel());
		condition.setActiveFlag("1");
		List<MdModel> list = mdModelService.findList(condition);
		if (list != null && !list.isEmpty()) {
			result.setCode(Constant.FAIL);
			result.setMsg("该原材料型号代码已经存在！");
			return result;
		}

		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/add";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		mdModelMes.setMaterialName(mdModel.getModelDescZh());
		mdModelMes.setMaterialType(mdModel.getModelType());
		mdModelMes.setOutsideDiameter(mdModel.getOutsideDiameter() == null ? ""
				: mdModel.getOutsideDiameter().toString());
		mdModelMes.setThickness(mdModel.getThickness() == null ? "" : mdModel
				.getThickness().toString());
//		货主（0：本公司 1：外公司）
		mdModelMes.setOwner(mdModel.getUserDefined3());
		mdModelMes.setMeasureUnit(mdModel.getUnit());
		// 0：无效 1：有效
		mdModelMes.setMarkAble("1");

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.setGmtCreate(now);
			mdModel.setCreateBy(mdModel.getCreateBy());
			mdModel.setActiveFlag("1");
			Resp resp = mdModelService.create(mdModel);

			return fromResp(resp);
		}
	}
	
	/**
	 * 修改记录【给erp用】
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("u/new")
	public TransResult updateErp(MdModel mdModel) {
//		自动转存大写
		mdModel.setModel(mdModel.getModel().toUpperCase());
		TransResult result = new TransResult();
		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/update/wms";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		mdModelMes.setMaterialName(mdModel.getModelDescZh());
		mdModelMes.setMaterialType(mdModel.getModelType());
		mdModelMes.setOutsideDiameter(mdModel.getOutsideDiameter() == null ? ""
				: mdModel.getOutsideDiameter().toString());
		mdModelMes.setThickness(mdModel.getThickness() == null ? "" : mdModel
				.getThickness().toString());
//		货主（0：本公司 1：外公司）
		mdModelMes.setOwner(mdModel.getUserDefined3());
		mdModelMes.setMeasureUnit(mdModel.getUnit());

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.preUpdate();
			mdModel.setLastModifiedBy(mdModel.getLastModifiedBy());
			Resp resp = mdModelService.updateErp(mdModel);
			return fromResp(resp);
		}
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 * 
	 * @param mdModel
	 * @return
	 */
	@PostMapping("d/new")
	public TransResult deleteErp(MdModel mdModel) {
		TransResult result = new TransResult();
		String url = mesProperties.getUrl()
				+ "/imes-provider/basicdata/meterial/data/update/wms";

		MdModelMes mdModelMes = new MdModelMes();
		mdModelMes.setMaterialCode(mdModel.getModel());
		// 0：无效 1：有效
		mdModelMes.setMarkAble("0");

		// 调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url,
				MediaType.APPLICATION_JSON_TYPE, JSON.toJSONString(mdModelMes));
		if (handResp.getCode() != null && handResp.getCode().equals("0")) {
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
			return result;
		} else {
			mdModel.setActiveFlag("0");
			mdModel.setGmtModified(new Date());
			mdModel.setLastModifiedBy(mdModel.getLastModifiedBy());
			Resp resp = mdModelService.deleteErp(mdModel);
			return fromResp(resp);
		}
	}

	
}
